package com.finstone.data.spider.eshop.pipeline;

import com.finstone.data.spider.utils.JsonUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EShopPagePipline extends BasePipeline {

    public void process(ResultItems paramResultItems, Task paramTask) {

        this.getJdbcTempate().execute("DELETE FROM goods_category");
        this.getJdbcTempate().execute("DELETE FROM goods_category_attrs");
        this.getJdbcTempate().execute("DELETE FROM goods");
        this.getJdbcTempate().execute("DELETE FROM goods_params");

        List<Map<String, Object>> list = paramResultItems.get("shopCategorty");
        List<Map<String, Object>> plist = paramResultItems.get("shopCategortyAttrs");

        String sql = "select props from czzfcg_ss.es_goods_type where type_id = 3";
        Map map = this.getJdbcTempate().queryForMap(sql);

        String prop_json = map.get("props").toString();
        List<Map<String, Object>> props_list = JsonUtils.fromJson(prop_json, List.class);
        for (Map<String, Object> prop_map : props_list) {
            String name = prop_map.get("name").toString();
            String options = prop_map.get("options").toString();
            List<Map<String, Object>> list_prop = new ArrayList<Map<String, Object>>();
            Map map1 = new HashMap();

            map1.put("category_id", paramResultItems.get("category_id"));
            map1.put("name", name);
            map1.put("value", options);
            list_prop.add(map1);
            this.getNamedParameterJdbcTemplate().batchUpdate("INSERT  INTO goods_c_attrs (category_id,name,value) values(:category_id,:name,:value)", paramMapper(list_prop));
        }

        this.getNamedParameterJdbcTemplate().batchUpdate("insert into goods_category_attrs (category_id,name,value) values (:category_id,:name,:value)",
                paramMapper(plist));
        this.getNamedParameterJdbcTemplate().batchUpdate("insert into goods_category (id,name1,name2,name3,url) values (:id,:name1,:name2,:name3,:url)",
                paramMapper(list));
    }

}
